package com.example.sls.dao.field;

import com.example.sls.pojo.field.Comments;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 评论表(Comments)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:54:59
 */
public interface CommentsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from comments where id=#{id}")
  Comments queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param comments 查询条件
   * @param pageSize 分页对象
   * @param offset   偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from comments where 1=1" +
          " and (id=#{comments.id} or (isnull(#{comments.id}) or #{comments.id}=0))" +
          " and (post_id=#{comments.postId} or (isnull(#{comments.postId}) or #{comments.postId}=0))" +
          " and (user_id=#{comments.userId} or (isnull(#{comments.userId}) or #{comments.userId}=0))" +
          " and (content=#{comments.content} or (isnull(#{comments.content}) or #{comments.content}=''))" +
          " and (created_at=#{comments.createdAt} or (isnull(#{comments.createdAt}) or #{comments.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Comments> queryAllByLimit(Comments comments, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from comments")
  long count();

  /**
   * 新增数据
   *
   * @param comments 实例对象
   * @return 影响行数
   */
  @Insert("insert into comments (id,post_id,user_id,content,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{postId}!=0 and !isnull(#{postId}),#{postId},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{content}!='' and !isnull(#{content}),#{content},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Comments comments);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param comments 实例对象
   * @return 影响行数
   */
  @Update("update comments set " +
          "post_id=if(isnull(#{postId}) or #{postId}=0,post_id,#{postId})," +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "content=if(isnull(#{content}) or #{content}='',content,#{content})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Comments comments);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from comments where id=#{id}")
  int deleteById(Integer id);
}

